import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * model 接收者类型维护
 * @date: 2018-7-31


 */
import { createPagination, getResponse } from 'utils/utils';
import { queryUnifyIdpValue } from 'cpaas-front/lib/services/api';
import { addReceiverType, assignListToReceiverType as _assignListToReceiverType, fetchReceiverType as _fetchReceiverType, queryAssignedList as _queryAssignedList, queryNoAssignUnitList as _queryNoAssignUnitList, queryNoAssignUserGroupList as _queryNoAssignUserGroupList, removeReceiverTypeList as _removeReceiverTypeList, updateReceiverType } from "../services/receiverTypeService";
/**
 * 定义接收者类型维护数据源及处理方法
 */

export default {
  namespace: 'receiverType',
  // model名称
  state: {
    list: [],
    // 数据展示列表
    pagination: {},
    // 分页器
    typeModes: [],
    // 接收组模式 值集
    newTypeModes: [] // 接收组模式 值集

  },
  effects: {
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_, _ref) {
      var call, put, enumTypeMode, relEnumTypeMode;
      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.next = 3;
              return call(queryUnifyIdpValue, 'CMSG.RECEIVER.TYPE_MODE');

            case 3:
              enumTypeMode = _context.sent;
              relEnumTypeMode = getResponse(enumTypeMode);

              if (!relEnumTypeMode) {
                _context.next = 8;
                break;
              }

              _context.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  typeModes: relEnumTypeMode,
                  newTypeModes: relEnumTypeMode.filter(function (t) {
                    return t.tag === 'ORG';
                  })
                }
              });

            case 8:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),

    /**
     * 获取接收者类型数据
     */
    fetchReceiverType: /*#__PURE__*/_regeneratorRuntime.mark(function fetchReceiverType(_ref2, _ref3) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchReceiverType$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              call = _ref3.call, put = _ref3.put;
              _context2.t0 = getResponse;
              _context2.next = 5;
              return call(_fetchReceiverType, payload);

            case 5:
              _context2.t1 = _context2.sent;
              result = (0, _context2.t0)(_context2.t1);

              if (!result) {
                _context2.next = 10;
                break;
              }

              _context2.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  list: result.content,
                  pagination: createPagination(result)
                }
              });

            case 10:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchReceiverType);
    }),

    /**
     * 更新接收者类型信息
     */
    updateType: /*#__PURE__*/_regeneratorRuntime.mark(function updateType(_ref4, _ref5) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function updateType$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call;
              _context3.next = 4;
              return call(updateReceiverType, payload);

            case 4:
              result = _context3.sent;
              return _context3.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, updateType);
    }),

    /**
     * 添加接收者类型信息
     */
    addType: /*#__PURE__*/_regeneratorRuntime.mark(function addType(_ref6, _ref7) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function addType$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;
              _context4.next = 4;
              return call(addReceiverType, payload);

            case 4:
              result = _context4.sent;
              return _context4.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, addType);
    }),
    // 接收者 模块配置
    queryAssignedList: /*#__PURE__*/_regeneratorRuntime.mark(function queryAssignedList(_ref8, _ref9) {
      var payload, call, id, query, result;
      return _regeneratorRuntime.wrap(function queryAssignedList$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call;
              id = payload.id, query = payload.query;
              _context5.next = 5;
              return call(_queryAssignedList, id, query);

            case 5:
              result = _context5.sent;
              return _context5.abrupt("return", getResponse(result));

            case 7:
            case "end":
              return _context5.stop();
          }
        }
      }, queryAssignedList);
    }),
    assignListToReceiverType: /*#__PURE__*/_regeneratorRuntime.mark(function assignListToReceiverType(_ref10, _ref11) {
      var payload, call, id, records, result;
      return _regeneratorRuntime.wrap(function assignListToReceiverType$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref10.payload;
              call = _ref11.call;
              id = payload.id, records = payload.records;
              _context6.next = 5;
              return call(_assignListToReceiverType, id, records);

            case 5:
              result = _context6.sent;
              return _context6.abrupt("return", getResponse(result));

            case 7:
            case "end":
              return _context6.stop();
          }
        }
      }, assignListToReceiverType);
    }),
    removeReceiverTypeList: /*#__PURE__*/_regeneratorRuntime.mark(function removeReceiverTypeList(_ref12, _ref13) {
      var payload, call, id, records, result;
      return _regeneratorRuntime.wrap(function removeReceiverTypeList$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref12.payload;
              call = _ref13.call;
              id = payload.id, records = payload.records;
              _context7.next = 5;
              return call(_removeReceiverTypeList, id, records);

            case 5:
              result = _context7.sent;
              return _context7.abrupt("return", getResponse(result));

            case 7:
            case "end":
              return _context7.stop();
          }
        }
      }, removeReceiverTypeList);
    }),
    queryNoAssignUnitList: /*#__PURE__*/_regeneratorRuntime.mark(function queryNoAssignUnitList(_ref14, _ref15) {
      var payload, call, id, query, result;
      return _regeneratorRuntime.wrap(function queryNoAssignUnitList$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref14.payload;
              call = _ref15.call;
              id = payload.id, query = payload.query;
              _context8.next = 5;
              return call(_queryNoAssignUnitList, id, query);

            case 5:
              result = _context8.sent;
              return _context8.abrupt("return", getResponse(result));

            case 7:
            case "end":
              return _context8.stop();
          }
        }
      }, queryNoAssignUnitList);
    }),
    queryNoAssignUserGroupList: /*#__PURE__*/_regeneratorRuntime.mark(function queryNoAssignUserGroupList(_ref16, _ref17) {
      var payload, call, id, query, result;
      return _regeneratorRuntime.wrap(function queryNoAssignUserGroupList$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref16.payload;
              call = _ref17.call;
              id = payload.id, query = payload.query;
              _context9.next = 5;
              return call(_queryNoAssignUserGroupList, id, query);

            case 5:
              result = _context9.sent;
              return _context9.abrupt("return", getResponse(result));

            case 7:
            case "end":
              return _context9.stop();
          }
        }
      }, queryNoAssignUserGroupList);
    })
  },
  reducers: {
    /**
     * 合并state状态数据,生成新的state
     */
    updateState: function updateState(state, _ref18) {
      var payload = _ref18.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};